// pages/myorder/myorder.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    assets:app.config.assets,
    orders:[],
    status:'all',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideShareMenu({
      complete: (res) => {},
    })
    if(options.status != undefined){
      this.setData({
        status:options.status
      })
    }
    this.getData();
  },
  getData: function(){
    var context = this;
    wx.request({
      url: app.config.server + '/api/orders?status='+this.data.status,
      header:{
        sessionkey: wx.getStorageSync('session_key')
      },
      success: res => {
        console.log(res)
        context.setData({
          orders:res.data
        });
      }
    })
  },
  

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  tapDelete:function(e){
    wx.request({
      url: app.config.server + '/api/orders/update',
      method:"POST",
      header:{
        sessionkey: wx.getStorageSync('session_key')
      },
      data:{
        order_id:e.currentTarget.dataset.id,
        status:0
      },
      success: res => {
        wx.redirectTo({
          url: '/pages/myorder/myorder?status='+this.data.status,
        })
      }
    })
  },

  tapPay:function(e){
    wx.request({
      url: app.config.server + "/api/orders/pay",
      method:"POST",
      header:{
        sessionkey: wx.getStorageSync('session_key')
      },
      data:{
        order_id:e.currentTarget.dataset.id
      },
      success:res => {
        console.log(res);
        wx.requestPayment({
          nonceStr: res.data.nonceStr,
          package: res.data.package,
          paySign: res.data.paySign,
          timeStamp: res.data.timeStamp + "",
          signType:"MD5",
          success: res => {
            console.log(res);
            wx.request({
              url: app.config.server + '/api/orders/update',
              method:"POST",
              header:{
                sessionkey: wx.getStorageSync('session_key')
              },
              data:{
                order_id:order_id,
                status:10
              },
              success: res => {
                wx.redirectTo({
                  url: '/pages/myorder/myorder?status='+this.data.status,
                })
              }
            })
          },
          complete: res => {
            console.log(res);
          }
        })
      }
    })
  },
  tapTab: function(e){
    this.setData({
      status:e.currentTarget.dataset.status
    });
    this.getData();
  },
  redirect:function(e){
    wx.redirectTo({
      url: e.currentTarget.dataset.url,
    })
  },
  navigate:function(e){
    wx.navigateTo({
      url: e.currentTarget.dataset.url,
    })
  }
})